package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BuyerConstant;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.JsonUtils;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.AgencyListViewBinder;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.AgencyListItem;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

public class AgencyListActivity extends BaseActivity {

    RecyclerView mRvGroupon;
    List<Object> mItems = new ArrayList<>();
    MultiTypeAdapter mMultiAdapter;
    AgencyListViewBinder grouponInfoViewBinder;
    LinearLayoutManager mManager;

    String grouponId;
    String productId;
    boolean isFinish;
    private String storeId;//基地店铺id


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            getIntent().putExtra(Constant.TYPE, savedInstanceState.getInt(Constant.TYPE));
            getIntent().putExtra("productId", savedInstanceState.getString("productId"));
            getIntent().putExtra("grouponId", savedInstanceState.getString("grouponId"));
            getIntent().putExtra("isFinish", savedInstanceState.getBoolean("isFinish"));

        }

        isFinish = getIntent().getBooleanExtra("isFinish",false);
        grouponId = getIntent().getStringExtra("grouponId");
        productId = getIntent().getStringExtra("productId");
        storeId = getIntent().getStringExtra("storeId");
        setContentView(R.layout.activity_agence_list);
        initView();

        reqAgency();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("grouponId", grouponId);
        outState.putString("productId", productId);
        outState.putBoolean("isFinish", isFinish);

        super.onSaveInstanceState(outState);
    }

    private void initView() {

        setToolbarTitle("代办列表");
        mRvGroupon = (RecyclerView) findViewById(R.id.rv_groupon_info);

        grouponInfoViewBinder = new AgencyListViewBinder();


        grouponInfoViewBinder.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object data) {
                intoGrouponInfoPage(grouponId,productId, (AgencyListItem) data);
            }
        });
        mManager = new LinearLayoutManager(this);

        mRvGroupon.setLayoutManager(mManager);

        mMultiAdapter = new MultiTypeAdapter();
        mMultiAdapter.register(AgencyListItem.class, grouponInfoViewBinder);


        mMultiAdapter.setItems(mItems);
        mRvGroupon.setAdapter(mMultiAdapter);

    }

    private void intoGrouponInfoPage(String grouponId,String productId,AgencyListItem item) {


        Intent intent = new Intent(this, GrouponInfoActivity.class);

        intent.putExtra("grouponId", grouponId);
        intent.putExtra("productId", productId);
        intent.putExtra("storeId", storeId);
        intent.putExtra("agencyItem",JsonUtils.toJson(item));


        if (isFinish){
            setResult(RESULT_OK,intent);
            finish();
        }else {
            startActivity(intent);
        }
    }

    private void updateView(List<AgencyListItem> list) {

        mItems.clear();
        if (list != null) {
            mItems.addAll(list);
        }
        mMultiAdapter.notifyDataSetChanged();
    }


    private void reqAgency() {
        RequestEntityMapSupply data = new RequestEntityMapSupply();
        data.setCmd(BuyerConstant.CMD_BUYER_PRODUCT_AGENT_STORES);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        data.setStoreId(BaseApplication.getInstance().getStoreId());

        data.putParameter("agentStoreId", BaseApplication.getInstance().getStoreId());
        data.putParameter("grouponId", grouponId);
        data.putParameter("productId", productId);

        requestBuyer(data, BuyerConstant.CMD_BUYER_PRODUCT_AGENT_STORES, new OnRequestListener<List<AgencyListItem>>() {
            @Override
            public List<AgencyListItem> jsonToObj(String responseStr) {
                return JsonUtils.fromJsonList(responseStr, AgencyListItem.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {

            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {

            }

            @Override
            public void onStart(Object tag) {

            }

            @Override
            public void onResponse(ResponseEntity<List<AgencyListItem>> response, Object tag) {
                List<AgencyListItem> responseElement = response.getResponseElement();

                if (responseElement != null) {

                    updateView(responseElement);
                }

            }
        });
    }


}
